package com.yangrong.thread.chapter7;

/**
 * 类锁的修饰（静态）方法和代码块：类锁的修饰（静态）方法和代码块：
 * 
 * @author YangRong
 * @date 2020年5月27日
 */
public class TestSynchronized1 {
	public void test1() {
		synchronized (TestSynchronized.class) {
			int i = 5;
			while (i-- > 0) {
				System.out.println(Thread.currentThread().getName() + " : " + i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException ie) {
				}
			}
		}
	}

	public static synchronized void test2() {
		int i = 5;
		while (i-- > 0) {
			System.out.println(Thread.currentThread().getName() + " : " + i);
			try {
				Thread.sleep(500);
			} catch (InterruptedException ie) {
			}
		}
	}

	public static void main(String[] args) {
		final TestSynchronized myt2 = new TestSynchronized();
		Thread test1 = new Thread(new Runnable() {
			public void run() {
				myt2.test1();
			}
		}, "test1");
		Thread test2 = new Thread(new Runnable() {
			public void run() {
				TestSynchronized1.test2();
			}
		}, "test2");
		test1.start();
		test2.start();
	}
}
